package com.mdc.mansion.core.modules.restaurant.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.basedata.dto.AppConfigDTO;
import com.mdc.mansion.core.modules.basedata.dto.RestaurantPrinterDTO;
import com.mdc.mansion.core.modules.restaurant.RestaurantExtService;
import com.mdc.mansion.core.modules.restaurant.dto.RestaurantDTO;
import com.mdc.mansion.core.modules.system.SysDictService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.restaurant.dao.RestaurantExtDao;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantExt;
import com.mdc.mansion.dataaccess.modules.system.entity.Dict;

/**
 * 餐厅扩展信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantExtServiceImpl extends CrudService<RestaurantExtDao, RestaurantExt> implements RestaurantExtService {
    @Autowired
    private SysDictService sysDictService;
    @Override
    public List<RestaurantExt> findImgList(String restaurantId) {
        RestaurantExt restaurantExt=new RestaurantExt();
        restaurantExt.setRestaurantId(restaurantId);
        restaurantExt.setType(RestaurantExt.TYPE.IMG.getVal());
        return this.findList(restaurantExt);
    }

    @Override
    @Transactional
    public void saveImgExt(Restaurant restaurant, RestaurantDTO restaurantDTO) {
        if(CollectionUtils.isEmpty(restaurantDTO.getImg())){return;}
        for(String img:restaurantDTO.getImg()){
            RestaurantExt restaurantExt=new RestaurantExt();
            restaurantExt.setRestaurantId(restaurant.getId());
            restaurantExt.setVal(img);
            restaurantExt.setType(RestaurantExt.TYPE.IMG.getVal());
            restaurantExt.setCreateBy(restaurant.getUpdateBy());
            restaurantExt.setUpdateBy(restaurant.getUpdateBy());
            this.save(restaurantExt);
        }
    }

    @Override
    @Transactional
    public void resaveImgExt(Restaurant restaurant, RestaurantDTO restaurantDTO) {
        List<RestaurantExt> restaurantExts = this.findImgList(restaurant.getId());
        if(CollectionUtils.isNotEmpty(restaurantExts)){
            for(RestaurantExt restaurantExt:restaurantExts){
                this.delete(restaurantExt);
            }
        }
        saveImgExt(restaurant,restaurantDTO);
    }

    @Override
    @Transactional
    public void saveRestaurantPrinter(RestaurantPrinterDTO restaurantPrinterDTO) {
        restaurantPrinterDTO.verfiy();
        String val = JSONObject.toJSONString(restaurantPrinterDTO);
        if(StringUtils.isNotBlank(restaurantPrinterDTO.getId())){ //修改
            RestaurantExt restaurantExt = this.get(restaurantPrinterDTO.getId());
            if(restaurantExt==null || StringUtils.equals(RestaurantExt.TYPE.PRINT.getVal(),restaurantExt.getType())){
                throw new BusinessException(I18nUtil.getMessage("param.error"));} // 参数错误
            restaurantExt.setVal(val);
            restaurantExt.setUpdateBy(RestaurantSupport.getCurrentOperationUser());
            this.updateSelective(restaurantExt);
        }else{ //添加
            RestaurantExt restaurantExt=new RestaurantExt();
            restaurantExt.setType(RestaurantExt.TYPE.PRINT.getVal());
            restaurantExt.setVal(val);
            restaurantExt.setRestaurantId(ContextHolder.currentRestaurant().getId());
            restaurantExt.setCreateBy(RestaurantSupport.getCurrentOperationUser());
            restaurantExt.setUpdateBy(RestaurantSupport.getCurrentOperationUser());
            this.save(restaurantExt);
        }
    }

    @Override
    @Transactional
    public void deleteRestaurantPrinter(RestaurantPrinterDTO restaurantPrinterDTO) {
        RestaurantExt restaurantExt = this.get(restaurantPrinterDTO.getId());
        if(restaurantExt==null){
            throw new BusinessException(I18nUtil.getMessage("param.error"));
        }
        this.delete(restaurantExt);
    }

    @Override
    public List<RestaurantPrinterDTO> getCurrentUserRestaurantPrinters() {
        RestaurantExt restaurantExt=new RestaurantExt();
        String id = ContextHolder.currentRestaurant().getId();
        restaurantExt.setRestaurantId(id);
        restaurantExt.setType(RestaurantExt.TYPE.PRINT.getVal());
        List<RestaurantExt> restaurantExts = this.findList(restaurantExt);
        List<RestaurantPrinterDTO> restaurantPrinterDTOs= Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(restaurantExts)){
            for(RestaurantExt ext:restaurantExts){
                RestaurantPrinterDTO restaurantPrinterDTO = JSONObject.parseObject(ext.getVal(), RestaurantPrinterDTO.class);
                restaurantPrinterDTO.setId(ext.getId());
                restaurantPrinterDTOs.add(restaurantPrinterDTO);
            }
        }
        return restaurantPrinterDTOs;
    }

    @Override
    public void saveDishUnit(Restaurant restaurant) {
        Dict dict = new Dict();
        dict.setType(AppConfigDTO.dishesUnit);
        List<Dict> list = sysDictService.findList(dict);
        if(CollectionUtils.isNotEmpty(list)){
            for(Dict dc:list){
                String value = dc.getValue();
                RestaurantExt restaurantExt=new RestaurantExt();
                restaurantExt.setRestaurantId(restaurant.getId());
                restaurantExt.setVal(value);
                restaurantExt.setType(RestaurantExt.TYPE.DISHES_UNIT.getVal());
                restaurantExt.setCreateBy(restaurant.getUpdateBy());
                restaurantExt.setUpdateBy(restaurant.getUpdateBy());
                this.save(restaurantExt);
            }
        }        
    }
}
